package cn.lkx.eduservice.controller;


import cn.lkx.commonutils.R;
import cn.lkx.eduservice.entity.EduTeacher;
import cn.lkx.eduservice.entity.vo.TeacherQuery;
import cn.lkx.eduservice.service.EduTeacherService;
import cn.lkx.eduservice.service.TeacherService;
import cn.lkx.servicebase.exceptionhandle.MyException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 后台控制器
 * </p>
 *
 * @author lkx
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin //解决跨域问题
public class EduTeacherController {

    @Autowired
    EduTeacherService eduTeacherService;
    @Autowired
    TeacherService teacherService;

    @GetMapping("findAll")  // '/' 加不加都行 http://localhost:8001/eduservice/teacher/findAll
    public R findAll(){
        List<EduTeacher> teachers = eduTeacherService.list(null);
        return R.ok().data("items",teachers);
    }

    /**
     * 删除讲师 逻辑删除
     */
    @DeleteMapping("delete/{id}")
    public R deleteTeacher(@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        return flag ? R.ok() : R.error();
    }
    /**
     * 分页测试  current 当前页， limit 每页记录数
     */
    @GetMapping("pageFind/{current}/{limit}")
    public R pageFind(
            @PathVariable long current,
            @PathVariable long limit){
        Page<EduTeacher> page = new Page<>(current,limit);
        eduTeacherService.page(page,null);

        /*try {
            int i=10/0;
        } catch (Exception e) {
            throw new MyException(20001,"执行了自定义异常...");
        }*/

        long total = page.getTotal();
        List<EduTeacher> teachers = page.getRecords();
        return R.ok().data("total",total).data("rows",teachers);
    }
    /**
     * 分页且多条件可变查询
     */
    @PostMapping("/pageAndConditions/{current}/{limit}")
    public R pageFindConditions(@PathVariable long current,
                                @PathVariable long limit,
                                //将前端传来的参数 自动封装成TeacherQuery 对象 (实际加上RequestBody较多：前端以JSON格式传来参数，Get请求没有请求体)
                                @RequestBody TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current,limit);
        teacherService.pageQueryConditions(page,teacherQuery);

        long total = page.getTotal();
        List<EduTeacher> teachers = page.getRecords();
        return R.ok().data("total",total).data("rows",teachers);
    }
    /**
     * 添加讲师
     */
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        return save ? R.ok() : R.error();
    }
    /**
     * 修改讲师 ,先查后改
     */
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean update = teacherService.updateById(eduTeacher);
        return update ? R.ok():R.error();
    }

}
